package awt;

import java.awt.*;
import java.awt.event.*;

/**
 * @author xiaoqi
 * @data 2021/05/16 0:17
 */
public class MenuDemo1 {
    //设置frame
    Frame frame = new Frame("MenuTest");
    //添加菜单条
    MenuBar bar = new MenuBar();
    //添加菜单组件
    Menu filemenu = new Menu("文件");
    Menu editormenu = new Menu("编辑");
    Menu formatmenu = new Menu("格式");
    //设置菜单选项
    MenuItem copy = new MenuItem("复制");
    MenuItem paste = new MenuItem("粘贴");
    MenuItem comment = new MenuItem("注释", new MenuShortcut(KeyEvent.VK_Q, true));
    MenuItem cancelcomment = new MenuItem("取消注释");
    TextArea ta = new TextArea(6, 30);

    public void init() {
        //组装格式,添加监视器
        comment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ta.append("注释" + "\n");
            }
        });
        formatmenu.add(comment);
        formatmenu.add(cancelcomment);
        //组装编辑
        editormenu.add(copy);
        editormenu.add(paste);
        editormenu.add(formatmenu);
        //组装菜单条
        bar.add(filemenu);
        bar.add(editormenu);
        //组装到frame中并完成相关内容
        frame.setMenuBar(bar);
        frame.add(ta);
        //调整大小，可视，位置
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        //设置可关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new MenuDemo1().init();
    }

}
